from rest_framework.views import APIView
from rest_framework.response import Response
from goods.models import *


class Search(APIView):
    def get(self, request):
        name = request.GET.get('q')
        All_the_goods = Goods.objects.all().order_by('-price')
        All_the_goods_data = []
        for i in All_the_goods:
            if name in i.sku_name:
                All_the_goods_data.append(i)
        if All_the_goods_data:
            res = GoodsSER(All_the_goods_data, many=True)
            return Response({'product': res.data, 'total': len(res.data)})
        else:
            # res = GoodsSER(All_the_goods, many=True)
            res = [{
                'cate': '',
                'count': '',
                'id': 1,
                'img': '',
                'instruction': '',
                'price': '',
                'selling_price': '',
                'sku_name': "无",
                'stock': '',
                'title': '',
            }]
            n = "本商店没有" + name + "这种垃圾东西"
            # return Response({'product': res.data, 'total': len(res.data), "msg": n})
            return Response({'product': res, 'total': 1, "msg": n})
